package com.hsgene.user.controller;

import com.google.common.collect.Lists;
import com.hsgene.common.util.result.ApiResult;
import com.hsgene.common.util.result.NullObject;
import com.hsgene.user.service.PasswordService;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 密码管理
 *
 * @author wangbing
 * @version 1.0, 2018/7/31
 */
@RestController
@RequestMapping("/password")
public class PasswordController {

    @Resource
    private PasswordService passwordService;

    /**
     * 更新用户密码，支持更新多个用户为相同密码
     *
     * @param userIds   用户id，多个用户已英文逗号分隔
     * @param password  新密码
     * @param decrypted 密码是否已解密（默认已解密）
     * @return 更新结果
     */
    @PutMapping
    public ApiResult<NullObject> updatePassword(
            @RequestParam("userIds") String userIds,
            @RequestParam("password") String password,
            @RequestParam(name = "decrypted", defaultValue = "true") Boolean decrypted) {
        List<String> ids = Lists.newArrayList(userIds.split(","));
        return passwordService.updatePassword(ids, password, decrypted);
    }

    /**
     * 更新供应商超级管理员密码
     *
     * @param supplierId 供应商id
     * @param password   新密码
     * @param decrypted  密码是否已解密（默认已解密）
     * @return 更新结果
     */
    @PutMapping("supplier/super")
    public ApiResult<NullObject> updateSupplierSuperPassword(
            @RequestParam("supplierId") Long supplierId,
            @RequestParam("password") String password,
            @RequestParam(name = "decrypted", defaultValue = "true") Boolean decrypted) {
        return passwordService.updateSupplierSuperPassword(supplierId, password, decrypted);
    }
}
